﻿using MaxStoreErp.Core.Exceptions;
using MaxStoreShop.Domain.Directories;
using MaxStoreShop.Logic.Services.Directories;
using MaxStoreShop.Logic.ServicesContracts.Directories;
using MaxStoreShop.Logic.Tests.FakeRepositories;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MaxStoreShop.Logic.Tests
{
    [TestClass]
    public class UserServiceTests
    {
        [TestMethod]
        public void AuthorizeUserShouldNormal()
        {
            var service = CreateInstance();
            service.AuthorizeUser(new StoreUser {SecretCode = "123"}, "123");
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void AuthorizeUserShouldThrowErpRuntimeExceptionIfPasswordInvalid()
        {
            var service = CreateInstance();
            service.AuthorizeUser(new StoreUser {SecretCode = "123"}, "321");
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void AuthorizeUserShouldThrowErpRuntimeExceptionIfUserIsNull()
        {
            var service = CreateInstance();
            service.AuthorizeUser(null, "123");
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void AuthorizeUserShouldThrowErpRuntimeExceptionIfPasswordNull()
        {
            var service = CreateInstance();
            service.AuthorizeUser(new StoreUser {SecretCode = "123"}, null);
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void AuthorizeUserShouldTrowErpRuntimeExceptionIfUserPasswordIsNullOrEmpty()
        {
            var service = CreateInstance();
            service.AuthorizeUser(new StoreUser(), "321");
        }

        [TestMethod]
        public void DeleteUserShouldNormal()
        {
            var service = CreateInstance();
            service.DeleteUser(new StoreUser {Id = 1});
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void DeleteUserShouldThrowErpRuntimeExceptionIfUserIsNull()
        {
            var service = CreateInstance();
            service.DeleteUser(null);
        }

        [TestMethod]
        public void SaveUserShouldNormal()
        {
            var service = CreateInstance();
            service.SaveUser(new StoreUser {FirstName = "Maxim", LastName = "Maximov", SecretCode = "123"});
        }

        [TestMethod]
        [ExpectedException(typeof(ErpRuntimeException))]
        public void SaveUserShouldThrowErpRuntimeExceptionIfUserIsNull()
        {
            var service = CreateInstance();
            service.SaveUser(null);
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void SaveUserThrowErpRuntimeExceptionIfFirstNameIsNullOrEmpty()
        {
            var service = CreateInstance();
            service.SaveUser(new StoreUser { FirstName = null, LastName = "Maximov", SecretCode = "123" });
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void SaveUserThrowErpRuntimeExceptionIfLastNameIsNullOrEmpty()
        {
            var service = CreateInstance();
            service.SaveUser(new StoreUser { FirstName = "Maxim", LastName = null, SecretCode = "123" });
        }

        [TestMethod]
        [ExpectedException(typeof (ErpRuntimeException))]
        public void SaveUserThrowErpRuntimeExceptionIfSecretCodeIsNullOrEmpty()
        {
            var service = CreateInstance();
            service.SaveUser(new StoreUser { FirstName = "Maxim", LastName = "Maximov", SecretCode = null });
        }

        static IUserService CreateInstance()
        {
            return new UserService { Repository = new UserRepositoryFake() };
        }
    }
}
